/* 
 * $Id$
 * CONFIDENTIAL AND PROPRIETARY. (c)  2007 Revolution Health Group LLC. All rights reserved. 
 * This source code may not be disclosed to others, used or reproduced without the written permission of Revolution Health Group.
 * 
 */
package org.javason.util;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

public final class Debug {
	/**
     * A reference to a string of characters that represents the version of this interface.
     */
    public static final String VERSION = "$Rev: 41030 $";
    
    private Debug() { }
    
	/**
	 * Log debugging output to log4j only if debugging is enabled
	 * @param logger the log4j logger for the class to be logged from
	 * @param format a format string @see http://java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html#syntax
	 * @param args Arguments referenced by the format specifiers in the format string.
	 * 
	 */
	public static void log(Logger logger, String format, Object ... args) {
		if (logger.isDebugEnabled()) {
			logger.debug(String.format(format, args));
		}
	}
	
	/**
	 * Log debugging output to log4j only if debugging is enabled
	 * @param logger the log4j logger for the class to be logged from
	 * @param throwable the exception to log, including its stack trace.
	 * @param format a format string @see http://java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html#syntax
	 * @param args Arguments referenced by the format specifiers in the format string.
	 * 
	 */
	public static void log(Logger logger, Throwable throwable, String format, Object ... args) {
		if (logger.isDebugEnabled()) {
			logger.debug(String.format(format, args), throwable);
		}
	}
	
	/**
	 * Output a "here" message for the function that this method was called from.
	 * @param logger the log4j logger for the class to be logged from
	 */
	public static void here(Logger logger) {
		if (logger.isDebugEnabled()) {
			Throwable throwable = new Throwable();
			StackTraceElement stEl[] = throwable.getStackTrace();
			StringBuilder methodName = new StringBuilder();
			if (stEl.length > 2) {
				methodName.append(stEl[1].getClassName());
				methodName.append('.');
				methodName.append(stEl[1].getMethodName());
				methodName.append('(').append(stEl[1].getLineNumber()).append(')');
			}
			throwable = null;
			logger.debug(String.format("%s: HERE", methodName));
		}
	}
	
	public static void logMap(Logger logger, Map map) {
		if (logger.isDebugEnabled()) {
			java.util.Set keys = map.keySet();
			Debug.log(logger, "DEBUG MAP -- ");
			for (Object key : keys) {
				if (map.get(key) != null){
					Debug.log(logger, "Key = %s, Value = %s", 
						key.toString(), map.get(key).toString());
				} else {
					Debug.log(logger, "Key = %s, Value is null", key.toString());
				}
			}
			Debug.log(logger, "End DEBUG MAP --");
		}
	}
	
	public static void logList(Logger logger, List<String> list) {
		if (logger.isDebugEnabled()) {
			for (String item : list) {
				Debug.log(logger, "list item = %s", item);
			}
		}
	}
}
